// src/stores/cartStore.js
import { defineStore } from "pinia";

export const useCartStore = defineStore("cart", {
  state: () => ({
    cartItems: [
      { quantity: 1, price: 100, id: 1, name: "商品1", isChecked: true },
      { quantity: 2, price: 200, id: 2, name: "商品2", isChecked: true },
      { quantity: 4, price: 300, id: 3, name: "商品3", isChecked: false },
    ],
  }),
  actions: {
    //删除
    removeItem(itemId) {
      this.cartItems = this.cartItems.filter((item) => item.id !== itemId);
    },
  },
  getters: {
    //总数量(检查是否选中)
    totalQuantity: (state) =>
      state.cartItems.reduce(
        (sum, item) => sum + (item.isChecked ? item.quantity : 0),
        0
      ),
    //总价(检查是否选中)
    totalPrice: (state) =>
      state.cartItems.reduce(
        (sum, item) => sum + (item.isChecked ? item.price * item.quantity : 0),
        0
      ),
  },
});
